// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_HR_LEAVE_APPLICATION
// * CreateDate    : 2013-08-28 11:00:56
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:56
// * LastChanger   : 2013-08-28 11:00:56
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Hr
{
/// <summary>
    /// HrLeaveApplication
    /// </summary>
    public partial class HrLeaveApplicationBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objHrLeaveApplicationEntity">Entity</param>
        /// <returns>Entity</returns>
        public static HrLeaveApplicationEntity GetHrLeaveApplication(HrLeaveApplicationEntity objHrLeaveApplicationEntity)
        {
            return DataAccess.SelectSingle<HrLeaveApplicationEntity>(objHrLeaveApplicationEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objHrLeaveApplicationEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertHrLeaveApplication(HrLeaveApplicationEntity objHrLeaveApplicationEntity)
        {
            return DataAccess.Insert(objHrLeaveApplicationEntity);
        }

        public static bool InsertHrLeaveApplication(List<HrLeaveApplicationEntity> objHrLeaveApplicationEntityList)
        {
            return DataAccess.Insert(objHrLeaveApplicationEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objHrLeaveApplicationEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateHrLeaveApplication(HrLeaveApplicationEntity objHrLeaveApplicationEntity)
        {
            return DataAccess.Update(objHrLeaveApplicationEntity);
        }

        public static bool UpdateHrLeaveApplication(List<HrLeaveApplicationEntity> objHrLeaveApplicationEntityList)
        {
            return DataAccess.Update<HrLeaveApplicationEntity>(objHrLeaveApplicationEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objHrLeaveApplicationEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteHrLeaveApplication(HrLeaveApplicationEntity objHrLeaveApplicationEntity)
        {
            return DataAccess.Delete(objHrLeaveApplicationEntity);
        }

        public static bool DeleteHrLeaveApplication(List<HrLeaveApplicationEntity> objHrLeaveApplicationEntityList)
        {
            return DataAccess.Delete<HrLeaveApplicationEntity>(objHrLeaveApplicationEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetHrLeaveApplicationAll()
        {
            return DataAccess.SelectAllDataSet<HrLeaveApplicationEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<HrLeaveApplicationEntity> GetHrLeaveApplicationAllList()
        {
            return DataAccess.SelectAll<HrLeaveApplicationEntity>();
        }
        #endregion


        public static DataTable GetLeaveApplicationsByCondition(HrLeaveApplicationEntity condition)
        {
            string sql =  @"Select  LA.*, P.EMPLOYEE_NAME , P.DEPARTMENT_NO,P.POSITION_ID
                                FROM HR_LEAVE_APPLICATION LA
                                LEFT JOIN hr_employee P ON P.EMPLOYEE_NO = LA.PERSON_ID
                                WHERE 1=1 ";

            if (condition != null)
            {

                if(!string.IsNullOrEmpty(condition.ApplicationId))
                    sql += " AND LA.APPLICATION_ID = '" + condition.ApplicationId + "'";
                if(!string.IsNullOrEmpty(condition.DepartmentId))
                    sql += " AND LA.DEPARTMENT_ID = '" + condition.DepartmentId + "'";

                if (!string.IsNullOrEmpty(condition.PersonId))
                    sql += " AND LA.PERSON_ID = '" + condition.PersonId + "'";
       
                 if (!string.IsNullOrEmpty(condition.LeaveType))
                    sql += " AND LA.LEAVE_TYPE = '" +condition.LeaveType + "'";
                
                if (!string.IsNullOrEmpty(condition.Status))
                    sql += " AND LA.STATUS = '" + condition.Status + "'";

                if (condition.ApplicationDate>DateTime.MinValue)
                {
                    sql += " AND LA.APPLICATION_DATE >= CONVERT(DATETIME,'" + condition.ApplicationDate.ToString("yyyy-MM-dd") + " 00:00:00')";
                }

                if (condition.StartDate > DateTime.MinValue && condition.EndDate > DateTime.MinValue)
                {
                    sql += " AND (( LA.START_DATE > CONVERT(DATETIME,'" + condition.StartDate.ToString("yyyy-MM-dd") + " 00:00:00')";
                    sql += " AND LA.START_DATE < CONVERT(DATETIME,'" + condition.EndDate.ToString("yyyy-MM-dd") + " 23:59:59') )";

                    sql += " or ( LA.END_DATE > CONVERT(DATETIME,'" + condition.StartDate.ToString("yyyy-MM-dd") + " 00:00:00')";
                    sql += " AND LA.END_DATE < CONVERT(DATETIME,'" + condition.EndDate.ToString("yyyy-MM-dd") + " 23:59:59') ))";
                }
                else if (condition.StartDate > DateTime.MinValue)
                {
                    sql += " AND  LA.START_DATE > CONVERT(DATETIME,'" + condition.StartDate.ToString("yyyy-MM-dd") + " 00:00:00')";
                   
                }else if (condition.EndDate > DateTime.MinValue)
                {
                    sql += " AND  LA.END_DATE > CONVERT(DATETIME,'" + condition.EndDate.ToString("yyyy-MM-dd") + " 23:59:59')";
                }
            }

            sql += " ORDER BY APPLICATION_DATE DESC";
           
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }


       
	}
	
	
}	

